package com.power200.showcase;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.power200.showcase.entity.Shop;
import com.power200.showcase.service.DataProvider;

import java.util.List;

public class ComboListViewActivity extends AppCompatActivity {

    private List<Shop> shopList;
    private DataProvider dataProvider = new DataProvider();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combo_list_view);

        System.out.println("combo onCreate");
        shopList = dataProvider.getShopList();
        System.out.println("shoplist="+shopList.size());
        ListView combListView = (ListView) findViewById(R.id.combListView);
        combListView.setAdapter(new MyAdapter(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_combo_list_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public final class ViewHolder {
        protected TextView combItem1;
        protected TextView combItem2;
        protected Button combButton;
    }

    public class MyAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;

        public MyAdapter(Context context) {
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return shopList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // position 是界面的position,不是data的position
            ViewHolder holder = null;
            if(convertView == null) {
                holder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.combo_list_item,null);
                holder.combItem1 = (TextView) convertView.findViewById(R.id.combItem1);
                holder.combItem2 = (TextView) convertView.findViewById(R.id.combItem2);
                holder.combButton = (Button) convertView.findViewById(R.id.combButton);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }

            holder.combItem1.setText(""+shopList.get(position).getId());
            holder.combItem2.setText(shopList.get(position).getName());
            holder.combButton .setText(shopList.get(position).getCategory());

            return convertView;
        }
    }
}
